﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameVLTK
{
    public class MySprite
    {
        private Texture2D[] _Textures;
        private Vector2 _TopLeft;
        private int _iTexture;
        private Vector2 _size;

        private int[] _FrameSequence;
        private int _nFrames;

        //Qui định khoảng thời gian giữa mỗi lần update
        private int _TimeSinceLastFrame = 0;
        private int _MilisecondsPerframe = 50;

        public int[] FrameSequence
        {
            get {
                return _FrameSequence; }
            set { 
                _FrameSequence = value;
                _nFrames = _FrameSequence.Length;
                _iTexture = 0;
            }
        }

        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private bool _Visible;

        #region Collision

        private int _CollisionRectX;

        public int CollisionRectX
        {
            get { return _CollisionRectX; }
            set { _CollisionRectX = value; }
        }
        private int _CollisionRectY;

        public int CollisionRectY
        {
            get { return _CollisionRectY; }
            set { _CollisionRectY = value; }
        }
        private int _CollisionRectWidth;

        public int CollisionRectWidth
        {
            get { return _CollisionRectWidth; }
            set { _CollisionRectWidth = value; }
        }
        private int _CollisionRectHeight;

        public int CollisionRectHeight
        {
            get { return _CollisionRectHeight; }
            set { _CollisionRectHeight = value; }
        }

        public bool Visible
        {
            get { return _Visible; }
            set { _Visible = value; }
        }

        #endregion

        public Vector2 TopLeft
        {
            get { return _TopLeft; }
            set { _TopLeft = value; }
        }

        public int iTexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }
      
        public Texture2D[] Textures
        {
            get 
            {
                return _Textures; 
            }
            set 
            { 
                _Textures = value;
                _iTexture = 0;
            }
        }

        private Color _colorFilter = Color.White;

        private Vector2 _logic;

        public Vector2 Logic
        {
            get { return _logic; }
            set { _logic = value; }
        }
        

        public Color ColorFilter
        {
            get { return _colorFilter; }
            set { _colorFilter = value; }
        }
               

        public MySprite(Texture2D[] _textures, Vector2 _position)
        {
            this.Textures = _textures;
            this.TopLeft = _position;

        }

        public void Update(GameTime gameTime)
        {
             _TimeSinceLastFrame+= gameTime.ElapsedGameTime.Milliseconds ;
             if (_TimeSinceLastFrame > _MilisecondsPerframe)
             {
                 _TimeSinceLastFrame = _TimeSinceLastFrame - _MilisecondsPerframe;
                 this._iTexture = (this._iTexture + 1) % this._nFrames;
             }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.Textures[this.FrameSequence[this.iTexture]],
                             new Vector2((int)this.TopLeft.X, (int)this.TopLeft.Y),
                             Color.White);
        }
    }
}
